/*===========================================================================+
 |   Copyright (c) 2001, 2005 Oracle Corporation, Redwood Shores, CA, USA    |
 |                         All rights reserved.                              |
 +===========================================================================+
 |  HISTORY                                                                  |
 +===========================================================================*/
package sza.oracle.apps.ap.oie.print.webui;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.Serializable;

import oracle.apps.fnd.common.VersionInfo;
import oracle.apps.fnd.framework.OAApplicationModule;
import oracle.apps.fnd.framework.OAException;
import oracle.apps.fnd.framework.webui.OAControllerImpl;
import oracle.apps.fnd.framework.webui.OAPageContext;
import oracle.apps.fnd.framework.webui.beans.OAWebBean;
import oracle.apps.xdo.oa.common.DocumentHelper;

import sza.oracle.apps.ap.oie.print.server.PrintHeaderVOImpl;

/**
 * Controller for ...
 */
public class SzaOiePrintSiglePageCO extends OAControllerImpl
{
  public static final String RCS_ID="$Header$";
  public static final boolean RCS_ID_RECORDED =
        VersionInfo.recordClassVersion(RCS_ID, "%packagename%");
        
    private static final String APPLICATION_SHORT_NAME = "SQLSZA";
    private static final String TEMPLATE_NAME = "PAYMENT_PRINT2";

  /**
   * Layout and page setup logic for a region.
   * @param pageContext the current OA page context
   * @param webBean the web bean corresponding to the region
   */
  public void processRequest(OAPageContext pageContext, OAWebBean webBean)
  {
    super.processRequest(pageContext, webBean);
    String reportHeaderId = pageContext.getParameter("reportHeaderId");
      OAApplicationModule am = pageContext.getApplicationModule(webBean);
     
            
     Serializable[] parameters = { reportHeaderId};
//     if (reportHeaderId !=null){
//         throw new OAException("reportHeaderId :"+reportHeaderId);
//     }
     am.invokeMethod("getSigleRecord",parameters);
      String str1 = pageContext.getTemporaryImageLocation();
                             String xmlStr = (String)am.invokeMethod("printSiglePayment");
                             String str3 = xmlStr.replaceAll("@###@",str1);
//                             System.err.println(str3);
                             ByteArrayOutputStream stream = new ByteArrayOutputStream();
                               try {
                                   stream.write(str3.getBytes());
                               } catch (IOException e) {
                                   // TODO
                               }
                               createSignatureImage(pageContext.getTemporaryImageLocation(),am);
                                  ByteArrayInputStream inputStream = new ByteArrayInputStream(stream.toByteArray());
                                  DocumentHelper.exportDocument(pageContext,
                                                                APPLICATION_SHORT_NAME,
                                                                TEMPLATE_NAME,
                                                                "zh",
                                                                "CN" ,
                                                                 inputStream,
                                                                 "PDF",
                                                                 null);
  }

  /**
   * Procedure to handle form submissions for form elements in
   * a region.
   * @param pageContext the current OA page context
   * @param webBean the web bean corresponding to the region
   */
  public void processFormRequest(OAPageContext pageContext, OAWebBean webBean)
  {
    super.processFormRequest(pageContext, webBean);
  }
    private void createSignatureImage(String relPath,OAApplicationModule am){
        PrintHeaderVOImpl headerVo = (PrintHeaderVOImpl)am.findViewObject("PrintHeaderVO1");
       
        String reportHeaders = headerVo.getHeaderIds();
        Serializable[] parameters = { reportHeaders, relPath};  
        System.err.println("relPath"+relPath);
        am.invokeMethod("createSignatureImage",parameters);
    }

}
